package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BuzTaskAwards;
import com.ruoyi.system.domain.BuzTaskAwardsAdd;
import com.ruoyi.system.domain.BuzTaskAwardsJackpot;
import com.ruoyi.system.mapper.BuzTaskAwardsJackpotMapper;
import com.ruoyi.system.service.IBuzTaskAwardsAddService;
import com.ruoyi.system.service.IBuzTaskAwardsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 追加奖品数量Controller
 * 
 * @author ruoyi
 * @date 2024-04-29
 */
@RestController
@RequestMapping("/system/buzTaskAwardsAdd")
public class BuzTaskAwardsAddController extends BaseController
{
    @Autowired
    private IBuzTaskAwardsAddService buzTaskAwardsAddService;
    
    @Autowired
    private IBuzTaskAwardsService buzTaskAwardsService;
    
    @Autowired
    private BuzTaskAwardsJackpotMapper buzTaskAwardsJackpotMapper;

    /**
     * 查询追加奖品数量列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BuzTaskAwardsAdd buzTaskAwardsAdd)
    {
        startPage();
        List<BuzTaskAwardsAdd> list = buzTaskAwardsAddService.selectBuzTaskAwardsAddList(buzTaskAwardsAdd);
        return getDataTable(list);
    }
    
    /**
     * 查询追加奖品数量列表
     */
    @GetMapping("/list2")
    public AjaxResult list2(BuzTaskAwardsAdd buzTaskAwardsAdd)
    {
        
        List<BuzTaskAwardsAdd> list = buzTaskAwardsAddService.selectBuzTaskAwardsAddList(buzTaskAwardsAdd);
        return AjaxResult.success(list);
    }

    /**
     * 导出追加奖品数量列表
     */
    @PreAuthorize("@ss.hasPermi('system:buzTaskAwardsAdd:export')")
    @Log(title = "追加奖品数量", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuzTaskAwardsAdd buzTaskAwardsAdd)
    {
        List<BuzTaskAwardsAdd> list = buzTaskAwardsAddService.selectBuzTaskAwardsAddList(buzTaskAwardsAdd);
        ExcelUtil<BuzTaskAwardsAdd> util = new ExcelUtil<BuzTaskAwardsAdd>(BuzTaskAwardsAdd.class);
        util.exportExcel(response, list, "追加奖品数量数据");
    }

    /**
     * 获取追加奖品数量详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:buzTaskAwardsAdd:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(buzTaskAwardsAddService.selectBuzTaskAwardsAddById(id));
    }

    /**
     * 新增追加奖品数量
     */

    @Log(title = "追加奖品数量", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BuzTaskAwardsAdd buzTaskAwardsAdd)
    {
    	
    	BuzTaskAwards taskAwards=new BuzTaskAwards();
    	taskAwards.setBuzTaskId(buzTaskAwardsAdd.getBuzTaskId());
    	taskAwards.setAwardsLevel(buzTaskAwardsAdd.getAwardsLevel());
    	List<BuzTaskAwards> taskAwardsList= buzTaskAwardsService.selectBuzTaskAwardsList(taskAwards);
    	if(taskAwardsList!=null&&taskAwardsList.size()>0) {
    		taskAwards=taskAwardsList.get(0);
    	}
    	
    	buzTaskAwardsAdd.setId(UUID.randomUUID().toString().replace("-",""));
    	buzTaskAwardsAddService.insertBuzTaskAwardsAdd(buzTaskAwardsAdd);
    	
    	
    	
    	//生成奖品明细
    	List<BuzTaskAwardsJackpot> buzTaskAwardsJackpots = new ArrayList();
        for (int i = 0; i < buzTaskAwardsAdd.getAddAwardNums(); i++) {
            BuzTaskAwardsJackpot buzTaskAwardsJackpot = new BuzTaskAwardsJackpot();
            buzTaskAwardsJackpot.setId(UUID.randomUUID().toString().replace("-",""));
            buzTaskAwardsJackpot.setBuzTaskId(buzTaskAwardsAdd.getBuzTaskId());
            buzTaskAwardsJackpot.setBuzTaskAwardsId(taskAwards.getId());
            buzTaskAwardsJackpot.setAwardName(taskAwards.getAwardName());
            buzTaskAwardsJackpot.setAwardsLevel(buzTaskAwardsAdd.getAwardsLevel());
            buzTaskAwardsJackpots.add(buzTaskAwardsJackpot);
        }
        buzTaskAwardsJackpotMapper.batchInsertBuzTaskAwardsJackpot(buzTaskAwardsJackpots);
    	
    	
    	//更新奖品总数和剩余奖品数
        taskAwards.setAwardNums(taskAwards.getAwardNums()+buzTaskAwardsAdd.getAddAwardNums());
        taskAwards.setLeftAwardNums(taskAwards.getLeftAwardNums()+buzTaskAwardsAdd.getAddAwardNums());
        buzTaskAwardsService.updateBuzTaskAwards(taskAwards);
    	
        return success(taskAwards);
    }

    /**
     * 修改追加奖品数量
     */
    @PreAuthorize("@ss.hasPermi('system:buzTaskAwardsAdd:edit')")
    @Log(title = "追加奖品数量", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuzTaskAwardsAdd buzTaskAwardsAdd)
    {
        return toAjax(buzTaskAwardsAddService.updateBuzTaskAwardsAdd(buzTaskAwardsAdd));
    }

    /**
     * 删除追加奖品数量
     */
    @PreAuthorize("@ss.hasPermi('system:buzTaskAwardsAdd:remove')")
    @Log(title = "追加奖品数量", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(buzTaskAwardsAddService.deleteBuzTaskAwardsAddByIds(ids));
    }
}
